#/usr/bin/tclsh


source [file dirname [info script]]/define.tcl
set packet_size		50


# List all available programming hardware, and select the USB-Blaster.
# (Note: this example assumes only one USB-Blaster is connected.)
#puts "Programming Hardware:"
foreach hardware_name [get_hardware_names] {
	#puts $hardware_name
	if { [string match "USB-Blaster*" $hardware_name] } {
		set usbblaster_name $hardware_name
	}
}
#puts "\nSelect JTAG chain connected to $usbblaster_name.\n";

# List all devices on the chain, and select the first device on the chain.
#puts "\nDevices on the JTAG chain:"
foreach device_name [get_device_names -hardware_name $usbblaster_name] {
	#puts $device_name
	if { [string match "@1*" $device_name] } {
		set test_device $device_name
	}
}

# Initiate a editing sequence
begin_memory_edit -hardware_name $usbblaster_name -device_name $test_device

# update data to sent to sdram 
set i 0
set code_hex 00000000
for {set i 1} {$i<$packet_size} {incr i} {		
	set code_hex  [format %4.8X $i]$code_hex
}
#puts $code_hex
	write_content_to_memory -instance_index 0 -start_address 0 -word_count 50 -content $code_hex -content_in_hex


update_cmd_mem  $SDRAM_SW_X_ADDR $SDRAM_SW_Y_ADDR 0 	$packet_size		5000
set cmd_error [send_cmd	$SDRAM_RW_CODE]
if { $cmd_error == 1} {
		puts "------------------------------fail to write on sdram !----------------------------------" 
		end_memory_edit
		exit -1

} 

write_read_req  0  $packet_size   5000 
send_cmd	$WRITE_CODE_R

	set done	0
	set count	0
	send_cmd	$START_CODE	
	while { $done ==  0 && $count <20 } {
		set read_status  [ read_content_from_memory -instance_index 1 -start_address 0 -word_count 1 ]
                set done [string index $read_status 0]
                incr count
		send_cmd	$START_CODE
		#puts $prog_done 
		#puts $read_status
	}
	if { $count > 19} {
		puts "sdram read command faield !" 
		end_memory_edit
		exit -1
		
	} 




	update_cmd_mem $SDRAM_SW_X_ADDR $SDRAM_SW_Y_ADDR 0 100 0
	set cmd_error [send_cmd $READ_CODE_R]
	
if { $cmd_error == 1} {
		puts "fail to read from sdram !" 
		end_memory_edit
		exit -1

} 


#End the editing sequence
end_memory_edit
